package com.addressbook.tests;

import static com.addressbook.tests.GroupDataGenerator.loadsGroupsFromXmlFile;
//import static com.addressbook.tests.GroupDataGenerator.loadsGroupsFromCsvFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.addressbook.fw.GroupData;
import com.addressbook.utils.SortedListOf;

public class GroupEditionTests extends TestBase {

	@DataProvider
    public static Iterator<Object[]> getValidGroupsFromFile() throws IOException {
            return wrapGroupDataForDataProvider(
                            loadsGroupsFromXmlFile(new File("groups.xml"))).iterator();
//							loadsGroupsFromCsvFile(new File("groups.txt"))).iterator();            
    }
	
	@Test(dataProvider = "getValidGroupsFromFile")
	public void testGroupEditionValidData(GroupData group) {
		//save old
		SortedListOf<GroupData> oldstate =  new SortedListOf<GroupData> (app.getHibernateHelper().listGroups());
		
		if (oldstate.size() == 0) throw new Error ("There is no groups to Edit.");
		
		Random rnd = new Random();		
		int index = rnd.nextInt(oldstate.size());
		
		app.getGroupHelper().editGroup(index,group);
	    
	    //save new
		SortedListOf<GroupData> newstate = app.getGroupHelper().getGroupsName();
	    
	    //check
	    assertThat(newstate, equalTo(oldstate.without(index).withAdded(group)));
	}

}
